package com.hudongwx.godoffood.activity.myActivity;

import android.content.Intent;
import android.support.annotation.IntegerRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.activity.Community_Activity.DetailsActivity;
import com.hudongwx.godoffood.fragment.HomeFragment;
import com.hudongwx.godoffood.fragment.communityFragment.viewPager.ImageActivity;
import com.hudongwx.godoffood.utils.API.API;
import com.hudongwx.godoffood.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

@ContentView(R.layout.activity_my_community)
public class MyCommunityActivity extends AppCompatActivity {
    @ViewInject(R.id.my_community_content_rv)
    private RecyclerView recyclerView;
    @ViewInject(R.id.my_community_head_icon_iv)
    private ImageView ivIcon;
    @ViewInject(R.id.my_community_head_tv)
    private TextView tvName;
    @ViewInject(R.id.my_community_head_icon_id)
    private TextView tvId;
    private MyAdapter myAdapter;
    private String TAG="MyCommunityActivity";
    private LinearLayoutManager linearLayoutManager;
    private String couldId;//记录id
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        //初始化头像
        tvName.setText(getIntent().getStringExtra("name"));
        couldId=getIntent().getStringExtra("couldId");
        tvId.setText(couldId );
        x.image().bind(ivIcon, getIntent().getStringExtra("icon"));

        loadData();
    }

    private void loadData(){
        Log.i(TAG, "loadData: 进入方法");
        RequestParams requestParams = new RequestParams(API.getMyCommunityUrl(couldId));
        x.http().get(requestParams, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: 我的数据：    "+data);
                    Log.i(TAG, "onSuccess: 我的数据：    "+data.length());
                    linearLayoutManager = new LinearLayoutManager(MyCommunityActivity.this);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    myAdapter = new MyAdapter(data);
                    recyclerView.setAdapter(myAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: ");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.i(TAG, "onCancelled: ");
            }

            @Override
            public void onFinished() {
                Log.i(TAG, "onFinished: ");
            }
        });
    }
    //社区中的item
    class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        JSONArray data;

        public MyAdapter(JSONArray data) {
            this.data = data;
            Log.i(TAG, "MyAdapter: "+data);
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.my_community_item,parent,false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            try {
                JSONObject item = data.getJSONObject(position);
                JSONArray imgData= item.getJSONArray("ListFile");
                Log.i(TAG, "onBindViewHolder: "+imgData);
                x.image().bind(holder.ivItemIcon,item.getString("UserIcon"));
                holder.tvItemName.setText(item.getString("UserName"));
                holder.tvItemTime.setText(item.getString("DateTime"));
                holder.tvItemContent.setText(item.getString("Title"));
                holder.gvContent.setAdapter(new GridAdapter(imgData));
                //当前页面的id
                holder.rlHead.setTag(item.getString("Id"));
                holder.rlHead.setOnClickListener(new RecycItemClick());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getItemCount() {
            return data.length();
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvItemName,tvItemTime,tvItemContent;
        ImageView ivItemIcon;
        GridView gvContent;
        RelativeLayout rlHead;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvItemName = (TextView) itemView.findViewById(R.id.my_community_item_name);
            tvItemTime = (TextView) itemView.findViewById(R.id.my_community_item_time);
            tvItemContent = (TextView) itemView.findViewById(R.id.my_community_item_say);
            ivItemIcon = (ImageView) itemView.findViewById(R.id.my_community_item_icon);
            gvContent= (GridView) itemView.findViewById(R.id.my_community_item_content);
            rlHead= (RelativeLayout) itemView.findViewById(R.id.my_community_item_head);
        }
    }
    //我的社区信息里面的图片
    class GridAdapter extends BaseAdapter{
        JSONArray imgData;
        private ArrayList<String> list = new ArrayList<>();
        public GridAdapter(JSONArray imgData) {
            this.imgData = imgData;
            addlist();
        }

        //将图片的地址添加进集合
        public void addlist() {
            for (int i = 0; i < imgData.length(); i++) {
                try {
                    list.add(imgData.getJSONObject(i).getString("Url"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        @Override
        public int getCount() {
            return imgData.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return imgData.getJSONObject(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            GridViewHolder gridViewHolder = null;
            if(convertView==null){
                gridViewHolder= new GridViewHolder();
                convertView= getLayoutInflater().inflate(R.layout.gridview_showdan_image, null);
                gridViewHolder.gridImg = (ImageView) convertView.findViewById(R.id.showdan_image3);
                convertView.setTag(gridViewHolder);
            }else {
                gridViewHolder = (GridViewHolder) convertView.getTag();
            }
            try {
                String imgUrl = imgData.getJSONObject(position).getString("Url");
                x.image().bind(gridViewHolder.gridImg,imgUrl);
                gridViewHolder.gridImg.setTag(list);
                gridViewHolder.gridImg.setOnClickListener(new RecycImgItemClick());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return convertView;
        }
    }
    class GridViewHolder{
        ImageView gridImg;
    }

    //消息点击事件
    class RecycItemClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MyCommunityActivity.this, DetailsActivity.class);
            intent.putExtra("postid",v.getTag()+"");
            startActivity(intent);
        }
    }
    //图片点击事件
    class RecycImgItemClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            ArrayList<String> data = (ArrayList<String>) v.getTag();
            Intent intent = new Intent(MyCommunityActivity.this, ImageActivity.class);
            intent.putStringArrayListExtra("list",data);
            startActivity(intent);
        }
    }
    //返回拦截
    @Event(R.id.my_community_iv_back)
    private void back(View view){
        finish();
    }

}
